<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of clienteControlador
 *
 * @author Marlon
 */
class cliente extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('pedido_model');
        $this->load->model('cliente_model');
        $this->load->model('Usuario_model');
        $this->load->model('persona_model');
    }
    public function registrarCliente()
    {
        $login=$this->loggedIn();
        $args['errorMsg'] = '';
        $args['title'] = 'Clientes';
        $args['content'] = 'cliente/cliente';        
        $session_data = $this->session->userdata['logged_in'];
        $args['username'] = $login['username'];
        $this->load->view('view',$args);       

    }
     public function buscarCliente()
    {
        $login=$this->loggedIn();
        $args['errorMsg'] = '';
        $args['title'] = 'Clientes';
        $args['content'] = 'cliente/buscarCliente';        
        $session_data = $this->session->userdata['logged_in'];
        $args['username'] = $login['username'];
        $this->load->view('view',$args);       

    }
    public function regresar(){
         redirect('home','refresh');
    }
     public function buscCliente(){
        $tipPer= $this->input->get('tipPer');
        $ruc = $this->input->get('ruc');
        $razon = $this->input->get('razon');
        $docu= $this->input->get('docu');
        $nombs = $this->input->get('nombs');
        $apellidos = $this->input->get('apellidos');
         $session_data = $this->session->userdata['logged_in'];
        $idUsuario=$session_data['user_id'];;
        $clientes=$this->persona_model->buscarcliente($idUsuario,$tipPer,$ruc,$razon,$docu,$nombs,$apellidos);
        $cad=sipcop_opteblaName('','table table-bordered','Código','Tipo Persona','RUC','Razón','Nombre (Repre)','Apellido (Repre)','Documento (Repre)','Editar','Eliminar');        
        foreach ($clientes as $client) { 
            $cad=$cad.(sipcop_tableRowEvent('',$client['codPer'],$client['tipo'],$client['ruc'],$client['razon'],$client['nomb'],$client['apellido'],$client['dni'],'<a class="btn btn-small" ><i class="icon-edit"></i></a>','<a class="btn btn-small" ><i class="icon-remove-sign"></i></a>'));
        }
        $cad=$cad.(sipcop_closeTable());
        echo $cad;        
    }
    public function guardarCliente(){
            
         $session_data = $this->session->userdata['logged_in'];
         $tipPer = $this->input->post('tipPer');
         $razon = $this->input->post('razon');
         $ruc = $this->input->post('ruc');
         $rublo = $this->input->post('rublo');
         $direc= $this->input->post('direc');
         $telef= $this->input->post('telef');         
         $genero = $this->input->post('gener');
         $nombres = $this->input->post('nombres');
         $apellid = $this->input->post('apellid');         
         $dni = $this->input->post('dni');                
         $telefcEL = $this->input->post('telefcEL');
         $email = $this->input->post('email');         
         $usuario=$session_data['user_id'];
         $persona=array('RazonSocial'=>$razon,'TipoDePersona'=>$tipPer,'RUC'=>$ruc,
             'Rublo'=>$rublo,'Telefono'=>$telef,'Direccion'=>$direc,
             'TelfCel'=>$telefcEL,'Genero'=>$genero,'DNI'=>$dni,
             'Nombres'=>$nombres,'Apellidos'=>$apellid,'Email'=>$email,);
         $idPersona=$this->Usuario_model->insertarPersona($persona);
         $cliente=array('IdCliente'=>$idPersona,'Usuario_IdUsuario'=>$usuario);                  
         $this->cliente_model->insertarCliente($cliente);
         //se regresa
         $login=$this->loggedIn();
        $args['errorMsg'] = '';
        $args['confirm'] = 'cliente';
        $args['mensaje'] = 'Se guardó al cliente';
        $args['title'] = 'Clientes';
        $args['content'] = 'cliente/cliente';
        $args['username'] = $login['username'];
        $this->load->view('view',$args); 
    }
}
?>
